package org.hgvc.webapp.ets.jsf.beans.mastersetup;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.faces.component.UIParameter;
import javax.faces.event.ActionEvent;

import org.hgvc.webapp.ets.bo.Country;
import org.hgvc.webapp.ets.bo.Race;
import org.hgvc.webapp.ets.constant.AdminConstant;
import org.hgvc.webapp.ets.constant.CommonConstant;
import org.hgvc.webapp.ets.spring.service.MasterSetupService;
import org.jocsb.base.AbstractBean;
import org.jocsb.base.exception.BaseException;


public class RaceBean extends AbstractBean{
	public static final String BEAN_NAME="raceBean";
	
	private MasterSetupService masterSetupService;
	private boolean catchError;
	private List<Race> raceList;
	private Race race;
	
	public List<Race> getRaceList() {
		return raceList;
	}
	public void setRaceList(List<Race> raceList) {
		this.raceList = raceList;
	}
	public Race getRace() {
		return race;
	}
	public void setRace(Race race) {
		this.race = race;
	}
	public void setMasterSetupService(MasterSetupService masterSetupService) {
		this.masterSetupService = masterSetupService;
	}
	
	public void init(){
		try{
			this.raceList = masterSetupService.getRaceList();	
		}catch(BaseException be){
			initErrorBean(be);
		}
	}
	
	public String navAddAction(){
		String outcome=AdminConstant.NAV_STATUS_SUCCESS;
		outcome=outcome.concat("_"+getRoleName());
		this.race=new Race();
		return outcome;
	}
	public String addAction(){
		String outcome=AdminConstant.NAV_STATUS_SUCCESS;
		outcome=outcome.concat("_"+getRoleName());
		if (catchError)
			outcome=null;
		return outcome;
	}
	public void addActionEvent(ActionEvent event){
		this.catchError=false;
		try{
			this.race.setCreatedBy(getThisUser());
			this.race.setCreatedDate(new Date());
			this.race.setIsDeleted(CommonConstant.NOT_DELETED);
			this.masterSetupService.saveRace(this.race);
			this.raceList = this.masterSetupService.getRaceList();
		}catch(BaseException be){
			this.catchError=true;
			initErrorBean(be);
		}
	}
	
	public String navEditAction(){
		String outcome=AdminConstant.NAV_STATUS_SUCCESS;
		outcome=outcome.concat("_"+getRoleName());
		if (catchError)
			outcome=null;
		return outcome;
	}
	public void navEditActionEvent(ActionEvent event){
		this.catchError=false;
		UIParameter param = (UIParameter) event.getComponent().findComponent("raceId");
		String raceId = (String)param.getValue();
		for(Race race:raceList){
			if(raceId.equalsIgnoreCase(race.getRaceId())){
				this.race = race;
				break;
			}
		}
	}
	
	public String updateAction(){
		String outcome=AdminConstant.NAV_STATUS_SUCCESS;
		outcome=outcome.concat("_"+getRoleName());
		if (catchError)
			outcome=null;
		return outcome;
	}
	public void updateActionEvent(ActionEvent event){
		this.catchError=false;
		try{
			this.race.setModifiedBy(getThisUser());
			this.race.setModifiedDate(new Date());
			this.masterSetupService.updateRace(this.race);
			this.raceList = this.masterSetupService.getRaceList();
		}catch(BaseException be){
			this.catchError=true;
			initErrorBean(be);
		}
	}
	
	public String deleteAction(){
		String outcome=AdminConstant.NAV_STATUS_SUCCESS;
		outcome=outcome.concat("_"+getRoleName());
		if (catchError)
			outcome=null;
		return outcome;
	}
	public void deleteActionEvent(ActionEvent event){
		this.catchError=false;
		try{
			List<Race> deleteList = new ArrayList<Race>();
			for (Race race : this.raceList){
				if (race.isMarkAsAction())
					deleteList.add(race);
			}
			this.masterSetupService.deleteRaces(deleteList);
			this.raceList = this.masterSetupService.getRaceList();
		}catch(BaseException be){
			this.catchError=true;
			initErrorBean(be);
		}
	}
	
	public String backToListAction(){
		String outcome=AdminConstant.NAV_STATUS_SUCCESS;
		outcome=outcome.concat("_"+getRoleName());
		return outcome;
	}
}
